/*
 * Powered By [ERP]
 * Since 2014 - 2016
 */

package com.erp.cloudfi.org.profitcenter.service;

import com.erp.cloudfi.org.profitcenter.dao.ProfitcenterDao;
import com.erp.cloudfi.org.profitcenter.model.MappingData;
import com.erp.cloudfi.org.profitcenter.model.Profitcenter;
import com.erp.cloudfi.org.profitcenter.model.ProfitcenterApiDataMap;
import com.erp.cloudfi.org.profitcenter.model.StoreOpenStatusEnum;
import com.erp.cloudfi.org.profitcenter.vo.query.ProfitcenterQuery;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.google.common.base.Preconditions.checkArgument;

/**
 * @author smn email:shimengnan007@163.com
 * @version 1.0
 * @since 1.0
 */
@Service
@Transactional
@Slf4j
public class ProfitcenterManager implements ProfitcenterService {

  @Autowired private ProfitcenterDao profitcenterDao;

  @Value("${env.name}")
  private String envName;

  @Override
  public List<Profitcenter> findCentersByGroup(String longnumber) {
    return profitcenterDao.findCentersByGroup(longnumber);
  }

  @Override
  public List<Profitcenter> getCentersByCorpIdForFront(String corpId, Integer status) {
    return null;
  }

  @Override
  public int removeById(String id, boolean checkCreateByCorpid) {
    return 0;
  }

  @Override
  public List<Profitcenter> getCentersByCorpId(String corpId, Integer status) {
    List<Profitcenter> profitcenterList = profitcenterDao.getCentersByCorpId(corpId, status);
    return filterByMcp(profitcenterList);
  }

  @Override
  public List<Profitcenter> getCentersByCorpidList(List<String> corpidList, Integer status) {
    List<Profitcenter> profitcenterList = profitcenterDao.getCentersByCorpidList(corpidList, status);
    return filterByMcp(profitcenterList);
  }

  private  List<Profitcenter> filterByMcp(List<Profitcenter> profitcenterList) {
    if ("prod".equals(envName)) {
      return profitcenterList.stream().filter(p -> isValid(p)).collect(Collectors.toList());
    }
    return profitcenterList;
  }

  private static boolean isValid(Profitcenter p) {
    return false;
  }

  @Override
  public List<Profitcenter> getCentersByCorpId(String corpId) {
    return getCentersByCorpId(corpId, 0);
  }

  @Override
  public List<String> getIds() {
    return profitcenterDao.getIds(null);
  }

  @Override
  public List<String> getIds2() {
    return profitcenterDao.getIds2(null);
  }

  @Override
  public ProfitcenterApiDataMap getProfitcenterApiMap(ProfitcenterQuery query) {
    return null;
  }

  @Override
  public void updateEffectdate(Profitcenter profitcenter) {

  }

  @Override
  public boolean syncAllToMcp() {
    return false;
  }

  @Override
  public void autoRemoveByCorpid(String id) {

  }

  @Override
  public Map<String, Profitcenter> getAllProfitcenterIdEntityMap() {
    return null;
  }

  @Override
  public List<String> getCenteridsByCorpId(String corpId) {
    return profitcenterDao.getCenteridsByCorpId(corpId);
  }

  @Override
  public List<String> getCenteridsNotCorpId(String corpId) {

    return profitcenterDao.getCenteridsNotCorpId(corpId);
  }

  public String findIdByNumber(String number) {
    return profitcenterDao.findIdByNumber(number);
  }
}
